﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Ds.DataBase;
using Ds.DataBase.Model;
using Newtonsoft.Json;

namespace Domain.RealiZation
{
    public class JiaoZhiGongtmpl : IJiaoZhiGong
    {
        public string AddJiaoZhiGong(string zh, string xm, int xb, string sjh, int ksid, int zwid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                if (!db.UserInfoContext.Any(s => s.username == zh))
                {
                    JiaoZhiGongInfo data = new JiaoZhiGongInfo();
                    var keshi = db.KeShiContext.SingleOrDefault(s => s.ksid == ksid);
                    var zhiwu = db.ZhiWeiContext.SingleOrDefault(s => s.zwid == zwid);
                    var zw = db.KeShiZhiWuContext.FirstOrDefault(s => s.zhiwu.zwid == zwid && s.KeShi.ksid == ksid);
                    data.keshi = keshi;
                    data.kszw = zw;
                    data.ShouJiHao = sjh;
                    data.Users = new UserInfo() { username = zh, password = "123456", userinfoId = Guid.NewGuid(), zt = 1 };
                    data.UpDateTime = DateTime.Now;
                    data.XingBie = xb;
                    data.XingMing = xm;
                    db.JiaoZhiGongInfoContext.Add(data);
                    db.SaveChanges();
                    return data.Users.userinfoId.ToString();
                }
                throw new Exception("账号已存在");
            }
        }

        public void UpdateJiaoZhiGong(string userid, string xm, int xb, string sjh, int ksid, int zwid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                Guid g = Guid.Parse(userid);
                var data = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.userinfoId == g);
                var keshi = db.KeShiContext.SingleOrDefault(s => s.ksid == ksid);
                var zhiwu = db.ZhiWeiContext.SingleOrDefault(s => s.zwid == zwid);
                var kszw = db.KeShiZhiWuContext.ToList();
                var zw = db.KeShiZhiWuContext.FirstOrDefault(s => s.zhiwu.zwid == zwid && s.KeShi.ksid == ksid);
                data.keshi = keshi;
                data.kszw = zw;
                data.ShouJiHao = sjh;
                data.XingBie = xb;
                data.XingMing = xm;
                //db.JiaoZhiGongInfoContext.Add(data);
                db.SaveChanges();
            }
        }

        public void DelJiaoZhiGong(string userid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var g = Guid.Parse(userid);
                var data = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.userinfoId == g);
                var userinfo = db.UserInfoContext.Single(s => s.userinfoId == g);
                db.Entry(data).State = EntityState.Deleted;
                db.Entry(userinfo).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        public void UpdatePassword(string username, string oldpassword, string newpassword)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username && s.Users.password == oldpassword);
                if (data != null)
                {
                    data.Users.password = newpassword;
                    db.SaveChanges();
                }
                else
                {
                    throw new Exception("旧密码不正确");
                }
            }
        }

        public string GetJiaoZhiGongs(string keyword, int ksid, int zwid, int pageIndex, int pageSize, out int pageTotal)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var kszw = db.KeShiZhiWuContext.ToList();
                var zw = db.ZhiWeiContext.ToList();
                var datalist = db.JiaoZhiGongInfoContext.Where(s => (keyword == null || keyword == "" || s.XingMing.ToUpper().Contains(keyword.ToUpper())) && (ksid == 0 || s.keshi.ksid == ksid) && (zwid == 0 || s.kszw.zhiwu.zwid == zwid));
                var keshi = db.KeShiContext.ToList();
                //if (!string.IsNullOrEmpty(keyword))
                //{
                //    datalist = db.JiaoZhiGongInfoContext.Where(s => s.XingMing.ToUpper().Contains(keyword.ToUpper()));
                //}
                pageTotal = datalist.Count();
                var query = datalist.OrderByDescending(s => s.UpDateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                List<Domain_JiaoZhiGong> lst = new List<Domain_JiaoZhiGong>();
                foreach (var item in query)
                {
                    Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                    model.keshi = new Domain_KeShi();
                    if (item.keshi != null)
                    {
                        model.keshi.ksid = item.keshi.ksid;
                        model.keshi.ksmc = item.keshi.ksmc;
                    }
                    model.ShouJiHao = item.ShouJiHao;
                    model.XingBie = item.XingBie;
                    model.XingMing = item.XingMing;
                    model.userinfoId = item.Users.userinfoId.ToString();
                    model.username = item.Users.username;
                    model.zgid = item.zgid;
                    if (item.kszw != null)
                    {
                        model.zwid = item.kszw.zhiwu.zwid;
                        model.zwmc = item.kszw.zhiwu.zwmc;
                    }

                    lst.Add(model);
                }

                return JsonConvert.SerializeObject(lst);
            }
        }

        public string GetJiaoZhiGongInfo(string userid)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                Guid id = Guid.Parse(userid);
                var data = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.userinfoId == id);
                var zw = db.ZhiWeiContext.ToList();
                var kszw = db.KeShiZhiWuContext.ToList();
                Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                model.keshi = new Domain_KeShi();
                model.keshi.ksid = data.keshi.ksid;
                model.keshi.ksmc = data.keshi.ksmc;
                model.ShouJiHao = data.ShouJiHao;
                model.XingBie = data.XingBie;
                model.XingMing = data.XingMing;
                model.userinfoId = data.Users.userinfoId.ToString();
                model.username = data.Users.username;
                model.zgid = data.zgid;
                if (data.kszw != null)
                {
                    model.zwid = data.kszw.zhiwu.zwid;
                    model.zwmc = data.kszw.zhiwu.zwmc;
                }
                return JsonConvert.SerializeObject(model);
            }
        }

        public string GetJiaoZhiGongInfoByUsername(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                var zw = db.ZhiWeiContext.ToList();
                var kszw = db.KeShiZhiWuContext.ToList();
                Domain_JiaoZhiGong model = new Domain_JiaoZhiGong();
                model.keshi = new Domain_KeShi();
                model.keshi.ksid = data.keshi.ksid;
                model.keshi.ksmc = data.keshi.ksmc;
                model.ShouJiHao = data.ShouJiHao;
                model.XingBie = data.XingBie;
                model.XingMing = data.XingMing;
                model.userinfoId = data.Users.userinfoId.ToString();
                model.username = data.Users.username;
                model.zgid = data.zgid;
                if (data.kszw != null)
                {
                    model.zwid = data.kszw.zhiwu.zwid;
                    model.zwmc = data.kszw.zhiwu.zwmc;
                }

                return JsonConvert.SerializeObject(model);
            }
        }

        public void ResetPassword(string username)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                var data = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == username);
                if (data != null)
                {
                    data.Users.password = "123456";
                    db.SaveChanges();
                }

            }

        }
    }
}
